/*
 * Copyright (c) 2017. 深圳掌控网络有限公司. All rights reserved.
 */

package com.yiqi.lottery.feature.find.presenter;

import android.text.TextUtils;
import com.suozhang.framework.entity.bo.PageData;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.utils.logger.Logger;
import com.yiqi.lottery.entity.bo.CircleBo;
import com.yiqi.lottery.entity.bo.PostCircleBo;
import com.yiqi.lottery.entity.bo.TokenBo;
import com.yiqi.lottery.entity.bo.UploadBo;
import com.yiqi.lottery.feature.find.contract.CircleContract;
import com.yiqi.lottery.feature.find.contract.WriteCircleContract;
import com.yiqi.lottery.feature.find.dependencies.circle.DaggerCirclePresenterComponent;
import com.yiqi.lottery.feature.find.dependencies.post.DaggerPostCirclePresenterComponent;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import javax.inject.Inject;
import java.util.List;

/**
 *      LIJUWEN
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/11 9:31
 */
public class WriteCirclePresenter implements WriteCircleContract.Presenter {

    private WriteCircleContract.View mView;

    @Inject

    WriteCircleContract.Model mModel;


    public WriteCirclePresenter(WriteCircleContract.View mView) {
        this.mView = mView;
        DaggerPostCirclePresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }




    @Override
    public void postQiniuToken() {
        mModel.getQiniuToken().compose(mView.bindToLife()).subscribe(new Observer<TokenBo>() {
            @Override
            public void onSubscribe(Disposable d) {
              mView.showLoading(d);
            }

            @Override
            public void onNext(TokenBo s) {
                mView.dismissLoading();
             mView.showQiniuTokenSuccess(s);
            }

            @Override
            public void onError(Throwable e) {mView.dismissLoading();
             mView.showQiniuTokenError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void postCircle(String content, List<UploadBo> uploadBos) {
        if (TextUtils.isEmpty(content)){
           mView.showUploadError("请填写内容！");
            return;
        }
        PostCircleBo postCircleBo=new PostCircleBo(content,uploadBos);
        mModel.postCircle(postCircleBo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                 mView.showUploadSuccess(s);mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showUploadError(e.getMessage());mView.dismissLoading();
            }

            @Override
            public void onComplete() {mView.dismissLoading();
            }
        });
    }
}
